/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        content: '',
        desc: '',
        selects: [],
        specList: [],
        entity: {
            student: {},
            studentClassList: [{}],
            parentEnterVoList: [{parent: {}, parentStudent: {}}]
        },
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        that.getSelectList();
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);
            return;
        }

        if (!empty(aGET.id)) {
            that.getInfoById(aGET.id);
        }
    },
    methods: {
        getSelectList: function () {
            var that = this;
            Get(that, _HOST + _API.organization_courseClass_getClassBy + "?schoolId=" + schoolId + "&companyId=" + companyId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        that.selects = response.list;
                    }
                });
        },
        selectCourse: function (index) {
            var that = this;
            $(".spec[data-index=" + index + "]").html('');
            console.log(that.entity.studentClassList);
            var classId = that.entity.studentClassList[index].classId;
            console.log(classId);
            if (empty(classId)) {
                return;
            }
            Get(that, _HOST + _API.organization_courseClass_getSpecBy + "?classId=" + classId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        that.specList = response.list;
                        var html = '<option value="">---请选择规格---</option>';
                        for (let i = 0; i < response.list.length; i++) {
                            html += '<option value="' + response.list[i].id + '">' + response.list[i].name
                                + '/' + response.list[i].unitName + ' 共' + response.list[i].lessonNum + '节课</option>';
                        }
                        $(".spec[data-index=" + index + "]").html(html);
                    }
                });
        },
        selectSpec: function (index) {
            var that = this;
            var saleId = that.entity.studentClassList[index].saleId;
            var obj = {};
            for (let i = 0; i < that.specList.length; i++) {
                if (saleId == that.specList[i].id) {
                    obj = that.specList[i];
                }
            }
            $(".price[data-index=" + index + "]").val(obj.price);
            that.entity.studentClassList[index].price = obj.price;
            $(".nowAmount[data-index=" + index + "]").val(obj.lessonNum);
            that.entity.studentClassList[index].nowAmount = obj.lessonNum;
            $(".allAmount[data-index=" + index + "]").val(obj.lessonNum);
            that.entity.studentClassList[index].allAmount = obj.lessonNum;
        },
        addOneClass: function (index) {
            var that = this;
            var entity = that.entity;
            entity.studentClassList.push({});
            that.entity = entity;
        },
        removeOneClass: function (index) {
            var that = this;
            var entity = that.entity;
            var length = entity.studentClassList.length;
            if (length == 1) {
                return;
            }
            entity.studentClassList = entity.studentClassList.splice(0, index)
                .concat(entity.studentClassList.splice(index + 1, length));
            that.entity = entity;
        },
        addOneRelation: function (index) {
            var that = this;
            var entity = that.entity;
            entity.parentEnterVoList.push({parent: {}, parentStudent: {}});
            that.entity = entity;
        },
        removeOneRelation: function (index) {
            var that = this;
            var entity = that.entity;
            var length = entity.parentEnterVoList.length;
            if (length == 1) {
                return;
            }
            entity.parentEnterVoList = entity.parentEnterVoList.splice(0, index)
                .concat(entity.parentEnterVoList.splice(index + 1, length));
            that.entity = entity;
        },
        save: function () {
            var that = this;
            console.log(that.entity);
            if (empty(aGET.id)) {
                that.add();
            } else {
                that.update();
            }
        },
        getInfoById: function (id) {
            var that = this;
            Get(that, _HOST + _API.userCenter_parentStudentEnter_getById + "?id=" + id + "&companyId=" + companyId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            console.log(eval("(" + response.data + ")"));
                            that.entity = eval("(" + response.data + ")");
                            // 判断是否为空
                            if (that.entity.studentClassList == null || that.entity.studentClassList.length == 0) {
                                that.entity.studentClassList = [{}];
                            } else {
                                for (let i = 0; i < that.entity.studentClassList.length; i++) {
                                    that.selectCourse(i);
                                }
                            }
                            if (that.entity.parentEnterVoList == null || that.entity.parentEnterVoList.length == 0) {
                                that.entity.parentEnterVoList = [{parent: {}, parentStudent: {}}];
                            }
                            // 选中规格
                            setTimeout(function () {
                                for (let i = 0; i < that.entity.studentClassList.length; i++) {
                                    $(".spec[data-index=" + i + "]").val(eval("(" + response.data + ")").studentClassList[i].saleId);
                                    $(".price[data-index=" + i + "]").val(eval("(" + response.data + ")").studentClassList[i].price);
                                    $(".nowAmount[data-index=" + i + "]").val(eval("(" + response.data + ")").studentClassList[i].nowAmount);
                                    $(".allAmount[data-index=" + i + "]").val(eval("(" + response.data + ")").studentClassList[i].allAmount);
                                }
                            }, 1000);
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                })
        },
        // 添加
        add: function () {
            var that = this;
            // 验证
            if (that.entity.studentClassList == null || that.entity.studentClassList.length == 0 || that.entity.studentClassList[0].classId == null) {
                layer.msg('至少添加一个学生课程信息！', {time: 1500, icon: 1});
                return;
            }
            if (that.entity.parentEnterVoList != null) {
                for (let i = 0; i < that.entity.parentEnterVoList.length; i++) {
                    if (that.entity.parentEnterVoList[i].parent.name != null && that.entity.parentEnterVoList[i].parent.wxTel == null) {
                        layer.msg('家长微信绑定电话不能为空！', {time: 1500, icon: 1});
                        return;
                    }
                }
                for (let i = 0; i < that.entity.parentEnterVoList.length; i++) {
                    that.entity.parentEnterVoList[i].parentStudent.cpcall = '家长';
                }
            }
            Post(that, _HOST + _API.userCenter_parentStudentEnter_add, that.entity, true, '')
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            layer.msg(response.msg, {time: 1500, icon: 1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                })
        },
        // 修改
        update: function () {
            var that = this;
            // 验证
            if (that.entity.studentClassList == null || that.entity.studentClassList.length == 0 || that.entity.studentClassList[0].classId == null) {
                layer.msg('至少添加一个学生课程信息！', {time: 1500, icon: 1});
                return;
            }
            if (that.entity.parentEnterVoList != null) {
                for (let i = 0; i < that.entity.parentEnterVoList.length; i++) {
                    if (that.entity.parentEnterVoList[i].parent.name != null && that.entity.parentEnterVoList[i].parent.wxTel == null) {
                        layer.msg('家长微信绑定电话不能为空！', {time: 1500, icon: 1});
                        return;
                    }
                }
                for (let i = 0; i < that.entity.parentEnterVoList.length; i++) {
                    that.entity.parentEnterVoList[i].parentStudent.cpcall = '家长';
                }
            }
            // console.log(param)
            Post(that, _HOST + _API.userCenter_parentStudentEnter_update, that.entity, true, '')
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            layer.msg(response.msg, {time: 1500, icon: 1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                })
        },
    },
});

